import { usePropsValue } from '../../hooks/use-props-value'
import { RadioGroupProvider } from './group-context'
import type { RadioGroupProps } from './type'

export const Group: React.FC<RadioGroupProps> = (props) => {
  const {
    value,
    defaultValue = null,
    disabled = false,
    onChange,
    children,
  } = props

  const [actValue, setActValue] = usePropsValue({
    value,
    defaultValue,
    onChange: (v) => {
      if (v === null || v === undefined) return
      onChange?.(v)
    },
  })

  return (
    <RadioGroupProvider
      value={{
        value: actValue === null ? [] : [actValue],
        disabled,
        uncheck: () => { /* void */ },
        check: (v) => {
          setActValue(v)
        },
      }}
    >
      {children}
    </RadioGroupProvider>
  )
}
